package com.adams.custom.page.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.DicBusinessDealFlow;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.BusinessDealFlowAction;
import com.adams.custom.page.core.BusinessDealFlowContext;
import com.adams.custom.page.core.dao.impl.DicBusinessDealFlowDaoImpl;
import com.adams.custom.page.core.service.BusinessDealFlowEngineService;
import com.adams.custom.page.util.ViewUtil;
import com.adams.utils.SpringBeanFactoryUtil;

/**
 * 
 * File Name : BusinessDealFlowEngineServiceImpl.java
 * 
 * @Description : 业务处理流调用引擎服务类实现
 * @author 904370 张汉辉
 */
public class BusinessDealFlowEngineServiceImpl implements
		BusinessDealFlowEngineService
{

	Logger logger = Logger.getLogger(BusinessDealFlowEngineServiceImpl.class
			.getName());

	/**
	 * Description : 业务处理流定义数据访问实现类 Author : 904370
	 */
	private DicBusinessDealFlowDaoImpl dicBusinessDealFlowDaoImpl;

	public void setDicBusinessDealFlowDaoImpl(
			DicBusinessDealFlowDaoImpl dicBusinessDealFlowDaoImpl)
	{
		this.dicBusinessDealFlowDaoImpl = dicBusinessDealFlowDaoImpl;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.custom.page.core.service.BusinessDealFlowEngineService
	 * #run(com.adams.custom.page.core.BusinessDealFlowContext)
	 */
	public Object run(BusinessDealFlowContext context) throws BusinessException
	{
		logger.info("run method run begin.");
		String actionType = context.getActionType();
		String tablecode = context.getTablecode();
		List<DicBusinessDealFlow> dicBusinessDealFlows = dicBusinessDealFlowDaoImpl
				.findByActionTypeAndTablecode(actionType, tablecode);
		if (null == dicBusinessDealFlows || dicBusinessDealFlows.size() == 0)
		{
			return null;
		}
		context.setDicBusinessDealFlows(dicBusinessDealFlows);
		List<BusinessDealFlowAction> beans = new ArrayList<BusinessDealFlowAction>();
		for (DicBusinessDealFlow dicBusinessDealFlow : dicBusinessDealFlows)
		{
			String beanname = dicBusinessDealFlow.getBeanname();
			if (ViewUtil.isNuLLStr(beanname))
			{
				logger.error("业务处理流定义出错，beanname属性不能为空！");
				throw new BusinessException(
						"business.deal.flow.config.exception");
			}
			Object bean = SpringBeanFactoryUtil.getBean(beanname);
			if (null == bean)
			{
				logger.error("业务处理流定义出错，业务处理" + beanname + "对象不存在！");
				throw new BusinessException(
						"business.deal.flow.config.exception");
			}
			if (!(bean instanceof BusinessDealFlowAction))
			{
				logger.error("业务处理流定义出错，业务处理" + beanname
						+ "对象必须实现BusinessDealFlowAction接口！");
				throw new BusinessException(
						"business.deal.flow.config.exception");
			}
			beans.add((BusinessDealFlowAction) bean);
		}
		Object resultObject = null;
		for (BusinessDealFlowAction bean : beans)
		{
			resultObject = bean.invoke(context, resultObject);
			context.next();
		}
		logger.info("run method run end.");
		return resultObject;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.custom.page.core.service.BusinessDealFlowEngineService
	 * #synchronizationCache()
	 */
	@Override
	public void synchronizationCache() throws BusinessException
	{
		logger.info("synchronizationCache method run begin.");
		dicBusinessDealFlowDaoImpl.synchronizationCache();
		logger.info("synchronizationCache method run end.");
	}
}
